package com.supreme.proxyserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonentity.mysql.primary.MBTest;
import com.supreme.commonmybatis.mapper.TestMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "test")
public class TestController {

    @Resource
    private TestMapper testMapper;

    @GetMapping(value = "search")
    public AjaxResp searchTest(@RequestParam(value = "testId") Long testId) {
        QueryWrapper<MBTest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", testId);
        final List<MBTest> mbTestList = this.testMapper.selectList(queryWrapper);
        return new AjaxResp(mbTestList);
    }

    @GetMapping(value = "searchAll")
    public AjaxResp searchTestAll() {
        List<MBTest> testList = this.testMapper.selectList(Wrappers.lambdaQuery());
        return new AjaxResp(testList);
    }

    @GetMapping(value = "searchPage")
    public AjaxResp searchTestPage() {
        QueryWrapper<MBTest> queryWrapper = new QueryWrapper<>();
        Page<MBTest> pageQuery = new Page<>(1, 8);
        Page<MBTest> selectPage = this.testMapper.selectPage(pageQuery, queryWrapper);

        return new AjaxResp(selectPage);
    }

    @GetMapping(value = "selectTestList")
    public AjaxResp selectTestList(MBTest test) {
        List<MBTest> testList = this.testMapper.selectTestList(test);
        return new AjaxResp(testList);
    }

    @PostMapping(value = "create")
    public AjaxResp createTest(@RequestBody MBTest test) {
        this.testMapper.insert(test);
        return new AjaxResp(test);
    }

    @PostMapping(value = "insert")
//    public RespRet insertTest(String firstName, String lastName, Integer testId, HttpServletRequest request) {
    public AjaxResp insertTest(@RequestParam(value = "firstName") String firstName,
                               @RequestParam(value = "lastName") String lastName,
                               @RequestParam(value = "testId") Integer testId) {
        MBTest mbTest = new MBTest();
        mbTest.setFirstName(firstName);
        mbTest.setLastName(lastName);
        int insert = this.testMapper.insert(mbTest);
        return new AjaxResp(insert);
    }

    @PutMapping(value = "update")
    public AjaxResp updateTest(@RequestBody MBTest test) {
        final int updateById = this.testMapper.updateById(test);
        return new AjaxResp(updateById);
    }

    @DeleteMapping(value = "delete/{id}")
    public AjaxResp deleteTest(@PathVariable(value = "id") Long id) {
        final int deleteById = this.testMapper.deleteById(id);
        return new AjaxResp(deleteById);
    }

    @DeleteMapping(value = "deleteMulti")
    public AjaxResp deleteTestMulti(@RequestParam(value = "ids") List<Long> ids) {
        final int deleteById = this.testMapper.deleteBatchIds(ids);
        return new AjaxResp(deleteById);
    }
}
